package com.tb.tbv1productservice.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.tb.v1.api.shoop.IGoodsTransactionService;
import com.tb.v1.api.shoop.ISpGoodsAttrService;
import com.tb.v1.api.shoop.ISpGoodsPicsService;
import com.tb.v1.api.shoop.ISpGoodsService;
import com.tb.v1.api.vo.SpGoodsVo;
import com.tb.v1.entity.SpGoods;
import com.tb.v1.entity.SpGoodsAttr;
import com.tb.v1.entity.SpGoodsPics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/23 0023 15:52
 * @Description:
 */
@Service
public class GoodsTransactionService implements IGoodsTransactionService {

    @Autowired
    private ISpGoodsService spGoodsService;
    @Autowired
    private ISpGoodsPicsService spGoodsPicsService;
    @Autowired
    private ISpGoodsAttrService spGoodsAttrService;

    /**
     * @Author tangBin
     * @Description 新增商品信息
     * @Date 2020/7/23 0023 13:11
     * @Param []
     * @return int
     **/
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int addGoods(SpGoodsVo vo) {
        SpGoods spGoods = new SpGoods();
        Long time = System.currentTimeMillis()/1000;
        spGoods.setAddTime(time);
        spGoods.setUpdTime(time);
        spGoods.setGoodsIntroduce(vo.getGoodsIntroduce());
        spGoods.setCatId(Integer.valueOf(vo.getGoodsCat().split(",")[2]));
        spGoods.setCatOneId(Integer.valueOf(vo.getGoodsCat().split(",")[0]));
        spGoods.setCatTwoId(Integer.valueOf(vo.getGoodsCat().split(",")[1]));
        spGoods.setCatThreeId(Integer.valueOf(vo.getGoodsCat().split(",")[2]));
        spGoods.setGoodsName(vo.getGoodsName());
        spGoods.setGoodsNumber(vo.getGoodsNumber());
        spGoods.setGoodsPrice(vo.getGoodsPrice());
        spGoods.setGoodsWeight(vo.getGoodsWeight());
        spGoods.setIsDel("0");
        //新增商品
        spGoodsService.saveEntity(spGoods);
        //新增商品属性
        List<Map> attrList = new ArrayList<>();
        for(Map map :vo.getAttrs()) {
            map.put("goodsId",spGoods.getGoodsId());
            attrList.add(map);
        }
        spGoodsAttrService.saveGoodsAttr(attrList);
        //新增商品图片
        List<Map> picList = new ArrayList<>();
        for(Map map :vo.getFileList()) {
            map.put("goodsId",spGoods.getGoodsId());
            picList.add(map);
        }
        spGoodsPicsService.saveGoodsPic(picList);
        return spGoods.getGoodsId() != null?1:0;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateGoods(SpGoodsVo vo) {
        SpGoods spGoods = new SpGoods();
        Long time = System.currentTimeMillis()/1000;
        spGoods.setUpdTime(time);
        spGoods.setGoodsIntroduce(vo.getGoodsIntroduce());
        spGoods.setCatId(Integer.valueOf(vo.getGoodsCat().split(",")[2]));
        spGoods.setCatOneId(Integer.valueOf(vo.getGoodsCat().split(",")[0]));
        spGoods.setCatTwoId(Integer.valueOf(vo.getGoodsCat().split(",")[1]));
        spGoods.setCatThreeId(Integer.valueOf(vo.getGoodsCat().split(",")[2]));
        spGoods.setGoodsName(vo.getGoodsName());
        spGoods.setGoodsNumber(vo.getGoodsNumber());
        spGoods.setGoodsPrice(vo.getGoodsPrice());
        spGoods.setGoodsWeight(vo.getGoodsWeight());
        spGoods.setGoodsId(vo.getGoodsId());
        Integer num = spGoodsService.updateEntity(spGoods);

        //修改商品属性
        for(Map map :vo.getAttrs()) {
            SpGoodsAttr attr = new SpGoodsAttr();
            attr.setAttrId(Integer.valueOf(map.get("attrId").toString()));
            attr.setAttrValue(map.get("attrVals").toString());
            attr.setGoodsId(vo.getGoodsId());
            Integer num2 = spGoodsAttrService.updateEntityAttr(attr);
            if(num2 == 0 ) {
                //新增商品属性
                spGoodsAttrService.saveEntity(attr);
            }
        }
        //修改图片
        for(Map map :vo.getFileList()) {
            SpGoodsPics pics = new SpGoodsPics();
            pics.setGoodsId(vo.getGoodsId());
            pics.setPicsBig(map.get("url").toString());
            spGoodsPicsService.updateEntity(pics);
        }
        return num;
    }
}
